package com.tenglong.teacher.ui.register;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.cuotibao.zhuoxiao.basics.utils.ClassPathResource;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.requestInfo.SysOfficeEntity;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.common.activity.BaseActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CompanyRegisterActivity extends BaseActivity {

    private EditText organizationName,organizationPhone,organizationAddress;
    private View tvComplete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_register);
        initView();
        setListeners();
    }

    private void setListeners() {
        tvComplete.setOnClickListener(this);
    }

    private void initView() {
        setTitle("企业机构注册");
        organizationName = findViewById(R.id.organizationName);
        organizationPhone = findViewById(R.id.organizationPhone);
        organizationAddress = findViewById(R.id.organizationAddress);
        tvComplete = findViewById(R.id.tvComplete);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tvComplete:
                if (TextUtils.isEmpty(organizationName.getText().toString().trim())){
                    ToastUtils.showCenterToast("机构名称不能为空");
                    return;
                }
                if (!ClassPathResource.isMobileNO(organizationPhone.getText().toString().trim())){
                    ToastUtils.showCenterToast("机构联系方式不正确");
                    return;
                }
                if (TextUtils.isEmpty(organizationAddress.getText().toString().trim())){
                    ToastUtils.showCenterToast("机构地址不能为空");
                    return;
                }
                organizationRegister();
                break;
        }
    }

    private void organizationRegister() {
        SysOfficeEntity entity = new SysOfficeEntity();
        entity.phone = organizationPhone.getText().toString().trim();
        entity.address = organizationAddress.getText().toString().trim();
        entity.name = organizationName.getText().toString().trim();
        CreateRequestEntity.getWebServiceLogin().registerOrganization(entity).enqueue(new Callback<BaseResult<String>>() {
            @Override
            public void onResponse(Call<BaseResult<String>> call, Response<BaseResult<String>> response) {
                if (ResultVerifier.isSucceed(response)){
                    ToastUtils.showCenterToast("机构注册成功");
                    finish();
                } else {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                }
            }

            @Override
            public void onFailure(Call<BaseResult<String>> call, Throwable t) {
                ResultVerifier.systerErrers(t);
            }
        });
    }
}
